﻿using System;
using System.Web;
using System.IO;
using System.Globalization;

namespace Eurodancelux.Website.HttpModules
{
    public class UrlRewriter : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {
            // Below is an example of how you can handle LogRequest event and provide 
            // custom logging implementation for it
            // context.LogRequest += new EventHandler(OnLogRequest);
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication context = (HttpApplication)sender;

            if (!HttpContext.Current.Request.Url.AbsoluteUri.EndsWith("/") && !HttpContext.Current.Request.RawUrl.Contains("."))
            {
                HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri + "/");
            }
            
            string url = HttpContext.Current.Request.RawUrl;

            if (url.Contains("/ru/"))
            {
                if (url.Contains("/ru/")) url = url.Replace("ru/", "");

                CultureInfo ci = new CultureInfo("ru-RU");
                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            }
            else if (url.Contains("/en/"))
            {
                if (url.Contains("/en/")) url = url.Replace("en/", "");

                CultureInfo ci = new CultureInfo("en-US");
                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            } 

            if (!url.Contains(".") && !url.Contains(".css") && !url.Contains(".js"))
            {
                if (!String.IsNullOrEmpty(url))
                {
                    if (url.EndsWith("/"))
                    {
                        url = url.Substring(0, url.Length - 1);
                    }

                    if (File.Exists(context.Request.PhysicalApplicationPath + url.Replace("/", "\\") + ".aspx"))
                    {
                        context.Context.RewritePath(url + ".aspx");
                    }
                    else
                    {
                        context.Context.RewritePath("/");
                    }
                }
                else
                {
                    context.Context.RewritePath("/");
                }
            }
        }

        #endregion

        public void OnLogRequest(Object source, EventArgs e)
        {
            //custom logging logic can go here
        }
    }
}
